package com.zjxf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.CmsStory;
import com.zjxf.base.bean.ro.RoCmsStory;
import com.zjxf.base.bean.vo.VoCmsStory;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.repository.CmsStoryRepository;
import com.zjxf.service.CmsStoryService;
import com.zjxf.utils.Conversion;
import com.zjxf.utils.DynamicSpecifications;
import com.zjxf.utils.PageUtils;
import com.zjxf.utils.SearchFilter;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 长征故事
 *
 * @author ZhangWei
 * @date 2021/4/6/006 19:12
 */
@Service
@AllArgsConstructor
@Transactional(rollbackOn = RuntimeException.class)
public class CmsStoryServiceImpl implements CmsStoryService {

    private final CmsStoryRepository cmsStoryRepository;

    public Optional<CmsStory> findById(Long id) {
        return cmsStoryRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public void addOrUpdate(VoCmsStory.addOrUpdate vo) {
        Optional<CmsStory> optionalCmsLmStory = findById(vo.getId());
        CmsStory cmsStory;
        if (optionalCmsLmStory.isPresent()) {
            cmsStory = optionalCmsLmStory.get();
            Conversion.voTurnPo(vo, cmsStory);
        } else {
            cmsStory = Conversion.voTurnPo(vo, new CmsStory());
        }
        cmsStoryRepository.save(cmsStory);
    }

    @Override
    public void delete(Long id) throws OperationException {
        CmsStory cmsStory = findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        cmsStory.setDeleteState(SysConst.DeleteState.DELETE_STATE.getCode());
        cmsStoryRepository.save(cmsStory);
    }

    @Override
    public CmsStory getEntity(Long id) throws OperationException {
        return findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
    }

    @Override
    public RoCmsStory.Details getEntityRo(CmsStory cmsStory) {
        return Conversion.poTurnRo(cmsStory, new RoCmsStory.Details());
    }

    @Override
    public Page<CmsStory> page(VoCmsStory.filter filter) {
        List<SearchFilter> searchFilters = new ArrayList<>();
        if (StringUtils.isNotBlank(filter.getKeywords())) {
            searchFilters.add(new SearchFilter("name", SearchFilter.Operator.LIKE, filter.getKeywords()));
        }
        Specification<CmsStory> specification = DynamicSpecifications.bySearchFilterByBasic(searchFilters, filter.getStartTime(), filter.getEndTime());
        return cmsStoryRepository.findAll(specification, PageUtils.buildPageRequest(filter.getPageNum(), filter.getPageSize()));
    }

    @Override
    public JSONObject pageRo(Page<CmsStory> page) {
        List<CmsStory> pageList = page.getContent();
        List<RoCmsStory.Page> roList = pageList.stream().map(item -> Conversion.poTurnRo(item, new RoCmsStory.Page())).collect(Collectors.toList());
        return PageUtils.page(page, roList);
    }
}
